package org.tp.framework.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpConnectionUtils {

	private Logger logger = LoggerFactory.getLogger(HttpConnectionUtils.class);

	private String method = "GET";

	private String url;

	private int connctionTimeout = 5000;

	private int connectionReadTimeout = 5000;

	private Map<String, String> httpRequestHeadParam;

	private HttpURLConnection connection;

	public HttpConnectionUtils setMethod(String method) {
		this.method = method;
		return this;

	}

	public HttpConnectionUtils setUrl(String url) {
		this.url = url;
		return this;
	}

	public HttpConnectionUtils setConnectionTimeout(int timeout) {
		this.connctionTimeout = timeout;
		return this;
	}

	public HttpConnectionUtils setConnectionreadTimeout(int readTimeout) {
		this.connectionReadTimeout = readTimeout;
		return this;
	}

	public HttpConnectionUtils setRequestHead(Map<String, String> headParam) {
		this.httpRequestHeadParam = headParam;
		return this;
	}

	public HttpConnectionUtils create() {
		try {
			HttpURLConnection reqConnection = null;
			URL reqUrl = new URL(url);
			reqConnection = (HttpURLConnection) reqUrl.openConnection();
			reqConnection.setConnectTimeout(connctionTimeout);
			reqConnection.setReadTimeout(connectionReadTimeout);
			reqConnection.setRequestMethod(method);
			if (httpRequestHeadParam != null && httpRequestHeadParam.size() != 0) {
				for (String requestKey : httpRequestHeadParam.keySet()) {
					reqConnection.setRequestProperty(requestKey, httpRequestHeadParam.get(requestKey));
				}
			}
			this.connection = reqConnection;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this;
	}

	public String doGet() {
		BufferedReader reader = null;
		String result = "";
		HttpURLConnection reqConnection = connection;
		try {
			reqConnection.connect();
			Map<String, List<String>> requestHeadMap = reqConnection.getHeaderFields();
			for (String key : requestHeadMap.keySet()) {
				logger.debug("doGet head map key is:{},value is:{}", key, requestHeadMap.get(key));
			}
			reader = new BufferedReader(new InputStreamReader(reqConnection.getInputStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				result += line;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}
		}
		return result;
	}

	public String doPost(Map<String, Object> param) {
		PrintWriter out = null;
		BufferedReader reader = null;
		String result = "";
		HttpURLConnection reqConnection = connection;
		try {
			reqConnection.setDoOutput(true);
			reqConnection.setDoInput(true);
			if (param != null && param.size() != 0) {
				out = new PrintWriter(reqConnection.getOutputStream());
				out.print(param);
				out.flush();
			}
			reqConnection.connect();
			Map<String, List<String>> requestHeadMap = reqConnection.getHeaderFields();
			for (String key : requestHeadMap.keySet()) {
				logger.debug("doPost head map key is:{},value is:{}", key, requestHeadMap.get(key));
			}
			reader = new BufferedReader(new InputStreamReader(reqConnection.getInputStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				result += line;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			reqConnection.disconnect();
		}
		return result;
	}

}
